<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/myblog/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/myblog/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/myblog/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/myblog/images/logo.svg" color="#222">

<link rel="stylesheet" href="/myblog/css/main.css">


<link rel="stylesheet" href="/myblog/lib/font-awesome/css/all.min.css">
  <link rel="stylesheet" href="/myblog/lib/pace/pace-theme-minimal.min.css">
  <script src="/myblog/lib/pace/pace.min.js"></script>

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"gao-qianwei.gitee.io","root":"/myblog/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="一、List简介    链表是一种物理存储单元上非连续、非顺序的存储结构，数据元素的逻辑顺序是通过链表中的指针链接次序实现的。">
<meta property="og:type" content="article">
<meta property="og:title" content="STL_list容器">
<meta property="og:url" content="https://gao-qianwei.gitee.io/myblog/2021/05/07/STL%EF%BC%9Alist%E5%AE%B9%E5%99%A8/index.html">
<meta property="og:site_name" content="学无止境">
<meta property="og:description" content="一、List简介    链表是一种物理存储单元上非连续、非顺序的存储结构，数据元素的逻辑顺序是通过链表中的指针链接次序实现的。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://gao-qianwei.gitee.io/myblog/myblog/2021/05/07/STL%EF%BC%9Alist%E5%AE%B9%E5%99%A8/1.png">
<meta property="article:published_time" content="2021-05-07T13:33:44.687Z">
<meta property="article:modified_time" content="2021-05-08T07:22:24.403Z">
<meta property="article:author" content="高乾威">
<meta property="article:tag" content="C++">
<meta property="article:tag" content="STL">
<meta property="article:tag" content="STL容器">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://gao-qianwei.gitee.io/myblog/myblog/2021/05/07/STL%EF%BC%9Alist%E5%AE%B9%E5%99%A8/1.png">

<link rel="canonical" href="https://gao-qianwei.gitee.io/myblog/2021/05/07/STL%EF%BC%9Alist%E5%AE%B9%E5%99%A8/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>STL_list容器 | 学无止境</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/myblog/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">学无止境</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/myblog/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/myblog/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/myblog/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/myblog/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/myblog/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
        <li class="menu-item menu-item-links">

    <a href="/myblog/links/" rel="section"><i class="fa fa-link fa-fw"></i>友链</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://gao-qianwei.gitee.io/myblog/2021/05/07/STL%EF%BC%9Alist%E5%AE%B9%E5%99%A8/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/myblog/images/avatar.png">
      <meta itemprop="name" content="高乾威">
      <meta itemprop="description" content="报我楼成秋望月，把君诗读夜回灯">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="学无止境">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          STL_list容器
        </h1>

        <div class="post-meta">

          
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2021-05-07 21:33:44" itemprop="dateCreated datePublished" datetime="2021-05-07T21:33:44+08:00">2021-05-07</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2021-05-08 15:22:24" itemprop="dateModified" datetime="2021-05-08T15:22:24+08:00">2021-05-08</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/myblog/categories/C/" itemprop="url" rel="index"><span itemprop="name">C++</span></a>
                </span>
                  ，
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/myblog/categories/C/STL/" itemprop="url" rel="index"><span itemprop="name">STL</span></a>
                </span>
            </span>

          
            <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv" style="display: none;">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span id="busuanzi_value_page_pv"></span>
            </span>
            <span class="post-meta-divider">|</span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h3 id="一、List简介"><a href="#一、List简介" class="headerlink" title="一、List简介"></a>一、List简介</h3><p>    链表是一种物理存储单元上非连续、非顺序的存储结构，数据元素的逻辑顺序是通过链表中的指针链接次序实现的。</p>
<span id="more"></span>

<p>    链表由一系列结点（链表中每一个元素称为结点）组成，结点可以在运行时动态生成。每个结点包括两个部分：一个是存储数据元素的数据域，另一个是存储下一个结点地址的指针域。</p>
<p>    相较于vector的连续线性空间，list的好处是每次插入或者删除一个元素，就是配置或者释放一个元素的空间。因此，list对于空间的运用有绝对的精准，一点也不浪费。而且，对于任何位置的元素插入或元素的移除，list永远是常数时间。</p>
<p>    List和vector是两个最常被使用的容器。</p>
<p>    list容器是一个<strong>双向链表容器</strong>，可高效地进行插入删除元素。</p>
<p>    list不可以随机存取元素，所以不支持at.(pos)函数与[]操作符。</p>
<p>    #include &lt;list&gt; </p>
 <img src="/myblog/myblog/2021/05/07/STL%EF%BC%9Alist%E5%AE%B9%E5%99%A8/1.png" alt="image-20210125101251344" style="zoom:50%;">

<h3 id="二、list对象的默认构造"><a href="#二、list对象的默认构造" class="headerlink" title="二、list对象的默认构造"></a>二、list对象的默认构造</h3><p>    list采用采用模板类实现,对象的默认构造形式：list&lt;T&gt; LIST; 如：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">list&lt;<span class="keyword">int</span>&gt; lstInt;      <span class="comment">//定义一个存放int的list容器。</span></span><br><span class="line">list&lt;<span class="keyword">float</span>&gt; lstFloat;   <span class="comment">//定义一个存放float的list容器。</span></span><br><span class="line">list&lt;string&gt; lstString;   <span class="comment">//定义一个存放string的list容器。              </span></span><br><span class="line"><span class="comment">//尖括号内还可以设置指针类型或自定义类型。</span></span><br></pre></td></tr></table></figure>



<h3 id="三、list头尾的添加移除操作"><a href="#三、list头尾的添加移除操作" class="headerlink" title="三、list头尾的添加移除操作"></a>三、list头尾的添加移除操作</h3><p>    list.push_back(elem);      //在容器尾部加入一个元素</p>
<p>    list.pop_back();       //删除容器中最后一个元素</p>
<p>    list.push_front(elem);   //在容器开头插入一个元素</p>
<p>    list.pop_front();       //从容器开头移除第一个元素</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">list&lt;<span class="keyword">int</span>&gt; lstInt;</span><br><span class="line">lstInt.<span class="built_in">push_back</span>(<span class="number">1</span>);</span><br><span class="line">lstInt.<span class="built_in">push_back</span>(<span class="number">3</span>);</span><br><span class="line">lstInt.<span class="built_in">push_back</span>(<span class="number">5</span>);</span><br><span class="line">lstInt.<span class="built_in">push_back</span>(<span class="number">7</span>);</span><br><span class="line">lstInt.<span class="built_in">push_back</span>(<span class="number">9</span>);</span><br><span class="line">lstInt.<span class="built_in">pop_front</span>();</span><br><span class="line">lstInt.<span class="built_in">pop_front</span>();</span><br><span class="line">lstInt.<span class="built_in">push_front</span>(<span class="number">11</span>);</span><br><span class="line">lstInt.<span class="built_in">push_front</span>(<span class="number">13</span>);</span><br><span class="line">lstInt.<span class="built_in">pop_back</span>();</span><br><span class="line">lstInt.<span class="built_in">pop_back</span>();</span><br><span class="line"><span class="comment">// lstInt    &#123;13,11,5&#125;</span></span><br></pre></td></tr></table></figure>



<h3 id="四、list的数据存取"><a href="#四、list的数据存取" class="headerlink" title="四、list的数据存取"></a>四、list的数据存取</h3><p>    list.front();  //返回第一个元素。</p>
<p>    list.back(); //返回最后一个元素。</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">list&lt;<span class="keyword">int</span>&gt; lstInt;</span><br><span class="line">lstInt.<span class="built_in">push_back</span>(<span class="number">1</span>);</span><br><span class="line">lstInt.<span class="built_in">push_back</span>(<span class="number">5</span>);</span><br><span class="line">lstInt.<span class="built_in">push_back</span>(<span class="number">9</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">int</span> iFront = lstInt.<span class="built_in">front</span>();	<span class="comment">//1</span></span><br><span class="line"><span class="keyword">int</span> iBack = lstInt.<span class="built_in">back</span>();		<span class="comment">//9</span></span><br></pre></td></tr></table></figure>



<h3 id="五、list与迭代器"><a href="#五、list与迭代器" class="headerlink" title="五、list与迭代器"></a>五、list与迭代器</h3><p>    list.begin();           //返回容器中第一个元素的迭代器。</p>
<p>    list.end();            //返回容器中最后一个元素之后的迭代器。</p>
<p>    list.rbegin();     //返回容器中倒数第一个元素的迭代器。</p>
<p>    list.rend();     //返回容器中倒数最后一个元素的后面的迭代器。</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span> (list&lt;<span class="keyword">int</span>&gt;::iterator it=lstInt.<span class="built_in">begin</span>(); it!=lstInt.<span class="built_in">end</span>(); ++it)	&#123;</span><br><span class="line">		cout &lt;&lt; *it&lt;&lt;cout &lt;&lt; <span class="string">&quot; &quot;</span>;</span><br><span class="line">	&#125;</span><br></pre></td></tr></table></figure>



<h3 id="六、list对象的带参数构造"><a href="#六、list对象的带参数构造" class="headerlink" title="六、list对象的带参数构造"></a>六、list对象的带参数构造</h3><p>    list(beg,end);  //构造函数将[beg, end)区间中的元素拷贝给本身。注意该区间是左闭右开的区间。</p>
<p>    list(n,elem);  //构造函数将n个elem拷贝给本身。</p>
<p>    list(const list &amp;lst); //拷贝构造函数。 </p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">list&lt;<span class="keyword">int</span>&gt; mlist1;</span><br><span class="line"><span class="function">list&lt;<span class="keyword">int</span>&gt; <span class="title">mlist2</span><span class="params">(<span class="number">10</span>, <span class="number">10</span>)</span></span>; <span class="comment">//有参构造</span></span><br><span class="line"><span class="function">list&lt;<span class="keyword">int</span>&gt; <span class="title">mlist3</span><span class="params">(mlist2)</span></span>;<span class="comment">//拷贝构造</span></span><br><span class="line"><span class="function">list&lt;<span class="keyword">int</span>&gt; <span class="title">mlist4</span><span class="params">(mlist2.begin(), mlist2.end())</span></span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> (list&lt;<span class="keyword">int</span>&gt;::iterator it = mlist4.<span class="built_in">begin</span>(); it != mlist4.<span class="built_in">end</span>(); it++) &#123;</span><br><span class="line">    cout &lt;&lt; *it &lt;&lt; <span class="string">&quot; &quot;</span>;</span><br><span class="line">&#125;</span><br><span class="line">cout &lt;&lt; endl;</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">结果：</span></span><br><span class="line"><span class="comment">10 10 10 10 10 10 10 10 10 10</span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure>



<h3 id="七、list的赋值"><a href="#七、list的赋值" class="headerlink" title="七、list的赋值"></a>七、list的赋值</h3><p>    list.assign(beg,end);  //将[beg, end)区间中的数据拷贝赋值给本身。注意该区间是左闭右开的区间。</p>
<p>    list.assign(n,elem); //将n个elem拷贝赋值给本身。</p>
<p>    list&amp; operator=(const list &amp;lst); //重载等号操作符</p>
<p>    list.swap(lst); // 将lst与本身的元素互换。</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">list&lt;<span class="keyword">int</span>&gt; lstIntA,lstIntB,lstIntC,lstIntD;</span><br><span class="line">lstIntA.<span class="built_in">push_back</span>(<span class="number">1</span>);</span><br><span class="line">lstIntA.<span class="built_in">push_back</span>(<span class="number">5</span>);</span><br><span class="line">lstIntA.<span class="built_in">push_back</span>(<span class="number">9</span>);</span><br><span class="line"></span><br><span class="line">lstIntB.<span class="built_in">assign</span>(lstIntA.<span class="built_in">begin</span>(),lstIntA.<span class="built_in">end</span>());		<span class="comment">//1 5 9</span></span><br><span class="line">lstIntC.<span class="built_in">assign</span>(<span class="number">5</span>,<span class="number">8</span>);							<span class="comment">//8 8 8 8 8</span></span><br><span class="line">lstIntD = lstIntA;							<span class="comment">//1 5 9</span></span><br><span class="line">lstIntC.<span class="built_in">swap</span>(lstIntD);						<span class="comment">//互换</span></span><br></pre></td></tr></table></figure>

<h3 id="八、list的大小"><a href="#八、list的大小" class="headerlink" title="八、list的大小"></a>八、list的大小</h3><p>    list.size();    //返回容器中元素的个数</p>
<p>    list.empty();      //判断容器是否为空</p>
<p>    list.resize(num);  //重新指定容器的长度为num，若容器变长，则以默认值填充新位置。如果容器变短，则末尾超出容器长度的元素被删除。</p>
<p>    list.resize(num, elem); //重新指定容器的长度为num，若容器变长，则以elem值填充新位置。如果容器变短，则末尾超出容器长度的元素被删除。</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">list&lt;<span class="keyword">int</span>&gt; lstIntA;</span><br><span class="line">lstIntA.<span class="built_in">push_back</span>(<span class="number">11</span>);</span><br><span class="line">lstIntA.<span class="built_in">push_back</span>(<span class="number">33</span>);</span><br><span class="line">lstIntA.<span class="built_in">push_back</span>(<span class="number">55</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> (!lstIntA.<span class="built_in">empty</span>())</span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">int</span> iSize = lstIntA.<span class="built_in">size</span>();		<span class="comment">//3</span></span><br><span class="line">    lstIntA.<span class="built_in">resize</span>(<span class="number">5</span>);			<span class="comment">//11 33 55 0 0</span></span><br><span class="line">    lstIntA.<span class="built_in">resize</span>(<span class="number">7</span>,<span class="number">1</span>);			<span class="comment">//11 33 55 0 0 1 1</span></span><br><span class="line">    lstIntA.<span class="built_in">resize</span>(<span class="number">2</span>);			<span class="comment">//11 33</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h3 id="九、list的插入"><a href="#九、list的插入" class="headerlink" title="九、list的插入"></a>九、list的插入</h3><p>    list.insert(pos,elem);  //在pos位置插入一个elem元素的拷贝，返回新数据的位置。</p>
<p>    list.insert(pos,n,elem);  //在pos位置插入n个elem数据，无返回值。</p>
<p>    list.insert(pos,beg,end);  //在pos位置插入[beg,end)区间的数据，无返回值。</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">list&lt;<span class="keyword">int</span>&gt; lstA;</span><br><span class="line">list&lt;<span class="keyword">int</span>&gt; lstB;</span><br><span class="line"></span><br><span class="line">lstA.<span class="built_in">push_back</span>(<span class="number">1</span>);</span><br><span class="line">lstA.<span class="built_in">push_back</span>(<span class="number">5</span>);</span><br><span class="line">lstA.<span class="built_in">push_back</span>(<span class="number">9</span>);</span><br><span class="line"></span><br><span class="line">lstB.<span class="built_in">push_back</span>(<span class="number">2</span>);</span><br><span class="line">lstB.<span class="built_in">push_back</span>(<span class="number">6</span>);</span><br><span class="line"></span><br><span class="line">lstA.<span class="built_in">insert</span>(lstA.<span class="built_in">begin</span>(), <span class="number">11</span>);		<span class="comment">//&#123;11, 1, 5, 9&#125;</span></span><br><span class="line">lstA.<span class="built_in">insert</span>(++lstA.<span class="built_in">begin</span>(),<span class="number">2</span>,<span class="number">33</span>);		<span class="comment">//&#123;11,33,33,1,5,9&#125;</span></span><br><span class="line">lstA.<span class="built_in">insert</span>(lstA.<span class="built_in">begin</span>() , lstB.<span class="built_in">begin</span>() , lstB.<span class="built_in">end</span>() );	<span class="comment">//&#123;2,6,11,33,33,1,5,9&#125;</span></span><br></pre></td></tr></table></figure>



<h3 id="十、list的删除"><a href="#十、list的删除" class="headerlink" title="十、list的删除"></a>十、list的删除</h3><p>    list.clear();     //移除容器的所有数据</p>
<p>    list.erase(beg,end); //**删除[beg,end)**区间的数据，返回下一个数据的位置。</p>
<p>    list.erase(pos);  //删除pos位置的数据，返回下一个数据的位置。</p>
<p>    lst.remove(elem);  //删除容器中所有与elem值匹配的元素。</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//删除区间内的元素</span></span><br><span class="line"><span class="comment">//lstInt是用list&lt;int&gt;声明的容器，现已包含按顺序的1,3,5,6,9元素。</span></span><br><span class="line">list&lt;<span class="keyword">int</span>&gt;::iterator itBegin=lstInt.<span class="built_in">begin</span>();</span><br><span class="line">++ itBegin;</span><br><span class="line">list&lt;<span class="keyword">int</span>&gt;::iterator itEnd=lstInt.<span class="built_in">begin</span>();</span><br><span class="line">++ itEnd;</span><br><span class="line">++ itEnd;</span><br><span class="line">++ itEnd;</span><br><span class="line">lstInt.<span class="built_in">erase</span>(itBegin,itEnd);</span><br><span class="line"><span class="comment">//此时容器lstInt包含按顺序的1,6,9三个元素。</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//假设 lstInt 包含1,3,2,3,3,3,4,3,5,3，删除容器中等于3的元素的方法一</span></span><br><span class="line"><span class="keyword">for</span>(list&lt;<span class="keyword">int</span>&gt;::iterator it=lstInt.<span class="built_in">being</span>(); it!=lstInt.<span class="built_in">end</span>(); )    <span class="comment">//小括号里不需写  ++it</span></span><br><span class="line">&#123;</span><br><span class="line">   <span class="keyword">if</span>(*it == <span class="number">3</span>)&#123;</span><br><span class="line">        it  =  lstInt.<span class="built_in">erase</span>(it);       <span class="comment">//以迭代器为参数，删除元素3，并把数据删除后的下一个元素位置返回给迭代器。</span></span><br><span class="line">         <span class="comment">//此时，不执行  ++it；  </span></span><br><span class="line">   &#125;</span><br><span class="line">   <span class="keyword">else</span>&#123;</span><br><span class="line">       ++it;</span><br><span class="line">   &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//删除容器中等于3的元素的方法二</span></span><br><span class="line">lstInt.<span class="built_in">remove</span>(<span class="number">3</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">//删除lstInt的所有元素</span></span><br><span class="line">lstInt.<span class="built_in">clear</span>();			<span class="comment">//容器为空</span></span><br></pre></td></tr></table></figure>



<h3 id="十一、list的反序排列"><a href="#十一、list的反序排列" class="headerlink" title="十一、list的反序排列"></a>十一、list的反序排列</h3><p>    lst.reverse();   //反转链表  </p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">list&lt;<span class="keyword">int</span>&gt; lstA;</span><br><span class="line"></span><br><span class="line">lstA.<span class="built_in">push_back</span>(<span class="number">1</span>);</span><br><span class="line">lstA.<span class="built_in">push_back</span>(<span class="number">3</span>);</span><br><span class="line">lstA.<span class="built_in">push_back</span>(<span class="number">5</span>);</span><br><span class="line">lstA.<span class="built_in">push_back</span>(<span class="number">7</span>);</span><br><span class="line">lstA.<span class="built_in">push_back</span>(<span class="number">9</span>);</span><br><span class="line"></span><br><span class="line">lstA.<span class="built_in">reverse</span>();			<span class="comment">//9 7 5 3 1</span></span><br></pre></td></tr></table></figure>

<h3 id="十二、拼接"><a href="#十二、拼接" class="headerlink" title="十二、拼接"></a>十二、拼接</h3><p>    list::splice实现list拼接的功能。将源list的内容部分或全部元素删除，拼插入到目的list。</p>
<p>    函数有以下三种声明：</p>
<ol>
<li><p> void splice ( iterator position, list&lt;T,Allocator&gt;&amp; x );  </p>
</li>
<li><p> void splice ( iterator position, list&lt;T,Allocator&gt;&amp; x, iterator it );</p>
</li>
<li><p> void splice ( iterator position, list&lt;T,Allocator&gt;&amp; x, iterator first, iterator last );</p>
</li>
</ol>
<p>解释：</p>
<p>    position 是要操作的list对象的迭代器；ist&lt;T Allocator&gt;&amp;x 被剪的对象</p>
<p>    对于一：会在position后把list&lt;T Allocator&gt;&amp;x所有的元素到剪接到要操作的list对象</p>
<p>    对于二：只会把it的值剪接到要操作的list对象中</p>
<p>    对于三：把first 到 last 剪接到要操作的list对象中 </p>
<p>样例：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;bits/stdc++.h&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	list&lt;<span class="keyword">int</span>&gt;li1,li2;</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;=<span class="number">4</span>;i++) li1.<span class="built_in">push_back</span>(i),li2.<span class="built_in">push_back</span>(i+<span class="number">10</span>);</span><br><span class="line">	<span class="comment">// li1 1 2 3 4</span></span><br><span class="line">	<span class="comment">// li2 11 12 13 14</span></span><br><span class="line">	list&lt;<span class="keyword">int</span>&gt;::iterator it=li1.<span class="built_in">begin</span>();</span><br><span class="line">	it++;</span><br><span class="line">	</span><br><span class="line">	</span><br><span class="line">	li1.<span class="built_in">splice</span>(it,li2);<span class="comment">//1 11 12 13 14 2 3 4</span></span><br><span class="line">	<span class="keyword">if</span>(li2.<span class="built_in">empty</span>()) cout&lt;&lt;<span class="string">&quot;li2 is empty&quot;</span>&lt;&lt;endl;</span><br><span class="line">	</span><br><span class="line">	</span><br><span class="line">	li2.<span class="built_in">splice</span>(li2.<span class="built_in">begin</span>(),li1,it);</span><br><span class="line">	cout&lt;&lt;*it&lt;&lt;<span class="string">&quot;   chen&quot;</span>&lt;&lt;endl;</span><br><span class="line">	<span class="comment">/*</span></span><br><span class="line"><span class="comment">	li1 1 11 12 13 14 3 4</span></span><br><span class="line"><span class="comment">	li2 2</span></span><br><span class="line"><span class="comment">	这里的it的值还是2  但是指向的已经是li2中的了 </span></span><br><span class="line"><span class="comment">	*/</span></span><br><span class="line">	</span><br><span class="line">	it=li1.<span class="built_in">begin</span>();</span><br><span class="line">	<span class="built_in">advance</span>(it,<span class="number">3</span>);<span class="comment">//advance 的意思是增加的意思，就是相当于 it=it+3;这里指向13</span></span><br><span class="line">	li1.<span class="built_in">splice</span>(li1.<span class="built_in">begin</span>(),li1,it,li1.<span class="built_in">end</span>()); <span class="comment">//13 14 3 4 1 11 12 可以发现it到li1.end()被剪贴到li1.begin()前面了 </span></span><br><span class="line">	<span class="keyword">for</span>(list&lt;<span class="keyword">int</span>&gt;::iterator it=li1.<span class="built_in">begin</span>();it!=li1.<span class="built_in">end</span>();++it) cout&lt;&lt;*it&lt;&lt;<span class="string">&quot;  &quot;</span>;</span><br><span class="line">	cout&lt;&lt;endl;</span><br><span class="line">	<span class="keyword">for</span>(list&lt;<span class="keyword">int</span>&gt;::iterator it=li2.<span class="built_in">begin</span>();it!=li2.<span class="built_in">end</span>();++it) cout&lt;&lt;*it&lt;&lt;<span class="string">&quot;  &quot;</span>;</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125; </span><br></pre></td></tr></table></figure>


    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/myblog/tags/C/" rel="tag"># C++</a>
              <a href="/myblog/tags/STL/" rel="tag"># STL</a>
              <a href="/myblog/tags/STL%E5%AE%B9%E5%99%A8/" rel="tag"># STL容器</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/myblog/2021/05/07/STL%EF%BC%9Adeque%E5%AE%B9%E5%99%A8/" rel="prev" title="STL_deque容器">
      <i class="fa fa-chevron-left"></i> STL_deque容器
    </a></div>
      <div class="post-nav-item">
    <a href="/myblog/2021/05/07/STL%EF%BC%9Amap%E5%92%8Cmultimap%E5%AE%B9%E5%99%A8/" rel="next" title="STL_map和multimap容器">
      STL_map和multimap容器 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%B8%80%E3%80%81List%E7%AE%80%E4%BB%8B"><span class="nav-text">一、List简介</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%BA%8C%E3%80%81list%E5%AF%B9%E8%B1%A1%E7%9A%84%E9%BB%98%E8%AE%A4%E6%9E%84%E9%80%A0"><span class="nav-text">二、list对象的默认构造</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%B8%89%E3%80%81list%E5%A4%B4%E5%B0%BE%E7%9A%84%E6%B7%BB%E5%8A%A0%E7%A7%BB%E9%99%A4%E6%93%8D%E4%BD%9C"><span class="nav-text">三、list头尾的添加移除操作</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%9B%9B%E3%80%81list%E7%9A%84%E6%95%B0%E6%8D%AE%E5%AD%98%E5%8F%96"><span class="nav-text">四、list的数据存取</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%BA%94%E3%80%81list%E4%B8%8E%E8%BF%AD%E4%BB%A3%E5%99%A8"><span class="nav-text">五、list与迭代器</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%85%AD%E3%80%81list%E5%AF%B9%E8%B1%A1%E7%9A%84%E5%B8%A6%E5%8F%82%E6%95%B0%E6%9E%84%E9%80%A0"><span class="nav-text">六、list对象的带参数构造</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%B8%83%E3%80%81list%E7%9A%84%E8%B5%8B%E5%80%BC"><span class="nav-text">七、list的赋值</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%85%AB%E3%80%81list%E7%9A%84%E5%A4%A7%E5%B0%8F"><span class="nav-text">八、list的大小</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%B9%9D%E3%80%81list%E7%9A%84%E6%8F%92%E5%85%A5"><span class="nav-text">九、list的插入</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%8D%81%E3%80%81list%E7%9A%84%E5%88%A0%E9%99%A4"><span class="nav-text">十、list的删除</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%8D%81%E4%B8%80%E3%80%81list%E7%9A%84%E5%8F%8D%E5%BA%8F%E6%8E%92%E5%88%97"><span class="nav-text">十一、list的反序排列</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%8D%81%E4%BA%8C%E3%80%81%E6%8B%BC%E6%8E%A5"><span class="nav-text">十二、拼接</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="高乾威"
      src="/myblog/images/avatar.png">
  <p class="site-author-name" itemprop="name">高乾威</p>
  <div class="site-description" itemprop="description">报我楼成秋望月，把君诗读夜回灯</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/myblog/archives/">
        
          <span class="site-state-item-count">91</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/myblog/categories/">
          
        <span class="site-state-item-count">27</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/myblog/tags/">
          
        <span class="site-state-item-count">59</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="http://github.com/GaoQianwei" title="GitHub → http:&#x2F;&#x2F;github.com&#x2F;GaoQianwei" rel="noopener" target="_blank">GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:2966807184@qq.com" title="E-Mail → mailto:2966807184@qq.com" rel="noopener" target="_blank">E-Mail</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://www.zhihu.com/people/xue-wu-zhi-jing-75-22" title="知乎 → https:&#x2F;&#x2F;www.zhihu.com&#x2F;people&#x2F;xue-wu-zhi-jing-75-22" rel="noopener" target="_blank">知乎</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://blog.csdn.net/weixin_45341339" title="CSDN → https:&#x2F;&#x2F;blog.csdn.net&#x2F;weixin_45341339" rel="noopener" target="_blank">CSDN</a>
      </span>
  </div>



      </div><div>
  <canvas id="canvasDiyBlock" style="width:60%;">当前浏览器不支持canvas，请更换浏览器后再试</canvas>
<script src="/myblog/js/custom/clock.js"></script>

</div>


    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 2021-04 到 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">高乾威</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a> 强力驱动
  </div>

<span class="post-meta-divider">|</span>

<div class="theme-info">
  <div class="powered-by"></div>
  <span class="post-count">全站共 117.2k 字</span>
</div>

<span class="post-meta-divider">|</span>
        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
</div>








      </div>
    </footer>
  </div>

  
  <script src="/myblog/lib/anime.min.js"></script>
  <script src="/myblog/lib/velocity/velocity.min.js"></script>
  <script src="/myblog/lib/velocity/velocity.ui.min.js"></script>

<script src="/myblog/js/utils.js"></script>

<script src="/myblog/js/motion.js"></script>


<script src="/myblog/js/schemes/pisces.js"></script>


<script src="/myblog/js/next-boot.js"></script>


  <script defer src="/myblog/lib/three/three.min.js"></script>
    <script defer src="/myblog/lib/three/canvas_lines.min.js"></script>


  




  
<script src="/myblog/js/local-search.js"></script>













  

  

</body>
</html>
